package com.example.controller;

import com.example.config.Utils;
import com.example.entity.Books;
import com.example.service.BooksService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/bookss")
@CrossOrigin(origins = "*")
public class BooksController {
    @Autowired
    private BooksService booksService;

    @GetMapping("/all")
    public List<Books> getAllBookss() {
        return booksService.getAllBookss();
    }
    @GetMapping("/list")
    public List<Books> listBookss(@RequestParam Map<String, Object> params) {
        System.out.println("listBookss by params==="+ params);
        return booksService.listBookssByParams(params);
    }

    @PostMapping("/add")
    public Books addBooks(@RequestBody  Books books) throws Exception {
        System.out.println("addBooks===");
        Utils.printValues(books);
        booksService.addBooks(books);
        return books;
    }

    @DeleteMapping("/delete/{id}")
    public String deleteBooks(@PathVariable Integer id) {
        System.out.println("deleteBooks by id==="+ id);
        booksService.deleteBooks(id);
        return "success";
    }

    @PostMapping("/deleteByIds")
    public String deleteBookssByIds(@RequestBody List<String> ids) {
        System.out.println("deleteBookss by ids==="+ ids);
        booksService.deleteBookss(ids);
        return "success";
    }

    @PostMapping("/update")
    public String updateBooks(@RequestBody Books books) throws Exception {
        Utils.printValues(books);
        booksService.updateBooks(books);
        return "success";
    }
}